//
//  SurMusicSelectViewController.m
//  wAudioShare
//
//  Created by 赵帅 on 14-1-11.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "SurMusicSelectViewController.h"
#import "PlayDeviceSelectViewController.h"

@interface SurMusicSelectViewController ()
{
    NSArray * musicArray;
    
     NSMutableArray * localMusicArray;
}

@end

@implementation SurMusicSelectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    musicArray = [[NSArray alloc] initWithObjects:@"1.mp3", nil];
    
    localMusicArray = [NSMutableArray array];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *fileList = [[NSArray alloc] init];
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    
    NSString *homeDir = NSHomeDirectory();
    NSString * kDocumentsPath = [homeDir stringByAppendingPathComponent:@"Documents"];
    
    fileList = [fileManager contentsOfDirectoryAtPath:kDocumentsPath error:&error];
    
    for(NSString * name in fileList)
    {
        if([[name pathExtension] isEqualToString:@"mp3"] || [[name pathExtension] isEqualToString:@"wav"] || [[name pathExtension] isEqualToString:@"wma"] ||[[name pathExtension] isEqualToString:@"ape"] || [[name pathExtension] isEqualToString:@"flac"])
        {
            [localMusicArray addObject:name];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return @"Surrounder";
}

#pragma mark -
#pragma mark UITableView Delegate & Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0) return @"Web";
    else if(section == 1) return @"Local";
    return @"";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)return [musicArray count];
    else
    return [localMusicArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    else
    {
        for(UIView * subview in [cell.contentView subviews])
        {
            [subview removeFromSuperview];
        }
    }
    
    // Configure the cell.
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if(indexPath.section == 0)
    {
        cell.textLabel.text = [musicArray objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [localMusicArray objectAtIndex:indexPath.row];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    
    NSString * musicURL = nil;
    
    if(indexPath.section == 0)
    {
        musicURL = [NSString stringWithFormat:@"http://www.wiimu.com:8020/upload/%@",musicArray[indexPath.row]];
    }
    else
    {
        musicURL = [NSString stringWithFormat:@"http://%@:%d/songs.m3u",[GlobalService sharedInstance].localIP,[GlobalService sharedInstance].localPort];
        
        //制作m3u文件
        NSMutableString * m3uStr = [NSMutableString string];
        NSString *homeDir = NSHomeDirectory();
        NSString * kDocumentsPath = [homeDir stringByAppendingPathComponent:@"Documents"];
        
        for(int i=indexPath.row;i<[localMusicArray count];i++)
        {
            [m3uStr appendFormat:@"http://%@:%d/%@\n",[GlobalService sharedInstance].localIP,[GlobalService sharedInstance].localPort,localMusicArray[i]];
        }
        
        NSData *data = [m3uStr dataUsingEncoding:NSUTF8StringEncoding];
        NSString *filename = [NSString stringWithFormat:@"%@/songs.m3u",kDocumentsPath];
        [data writeToFile: filename atomically: NO];
    }
    
    PlayDeviceSelectViewController * controller = [[PlayDeviceSelectViewController alloc] initWithNibName:@"PlayDeviceSelectViewController" bundle:nil];
    controller.musicName = musicURL;
    [self.navigationController pushViewController:controller animated:YES];
}
@end
